package net.coding.li709.materialrefresher.utils;

import net.coding.li709.materialrefresher.keys.FlanData;
import org.spongepowered.api.GameRegistry;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityType;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.entity.PlayerInventory;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.extent.Extent;

import java.util.Optional;

public class ItemUtils {

    private static GameRegistry registry;

    static {
        registry = Sponge.getRegistry();
    }

    /**
     * 根据物品名获取物品
     * @param itemName modid:name
     */
    public static Optional<ItemStack> getItemStack(String itemName){
        Optional<ItemType> optionalItemType = registry.getType(ItemType.class, itemName);
        return optionalItemType.map(itemType -> ItemStack.builder().itemType(itemType).build());
    }


    /**
     * 添加物品到玩家背包，返回是否成功
     */
    public static InventoryTransactionResult addItem(ItemStack itemStack, PlayerInventory playerInventory){

       return playerInventory.offer(itemStack);
    }
}
